﻿using System;
using System.IO;
using ArchLord2Commons.Network;
using ArchLord2Commons.Utils;

namespace AL2Login.Network.Frames.Send
{
    public class SfRequestConnection : AALNetFrame
    {
        private readonly int _serverId;
        private readonly string _token;

        public SfRequestConnection(int serverId, string token)
        {
            if (token.Length < 18 & token.Length > 18)
                throw new Exception("incorrect token length");
            
            _serverId = serverId;
            _token = token;            
        }
        public override void ProcessWrite()
        {
            PutInt32(_serverId);

            byte[] bodyBlob;

            using (var str = new MemoryStream())
            using (var writer = new BinaryWriter(str))
            {              
                writer.WriteS(_token, 36);

                bodyBlob = str.ToArray();

                if (Buffer.ByteLength(bodyBlob) > 36) //debug code, dont touch
                    Array.Resize(ref bodyBlob, 36);

                str.Position = Buffer.ByteLength(bodyBlob);

                writer.Write("0080bd941066ddd3a27849f192ff5dded43ca32b488aad243bf0196d".ToBytes()); //unk, client parameter`s?

                writer.WriteS("127.0.0.1", 14);
                
                writer.Write("3ccc00000000".ToBytes());

                bodyBlob = str.ToArray();
            }

            PutArray(bodyBlob);
        }
    }
}
//9213 94010000 serverId
//366e383467386436646e666e623668646e386778676e386a6a386266306434643438643900 password salt
//300d58c4e002575896c93beb1f39869b6bdb09bad8f162784a94a0 unk
//3132372e302e302e3100d000000000 ip adress
//3132372e302e303ccc00000000

//9213 opCode
//65000000 serverId
//3468626a6b3864326b786b3262633264326b63333966326a68686666666338653365346400 password salt
//66c6f823ee2a2ef9e59c1be25181a2677752c3d1393253cb06e41b unk
//3231312e34332e3134362e3636001c3600000000 ip addrs

//9213 opCode
//65000000 serverId
//35613038303630616c663538696c626338357a36306c353862667a36303538626364313000 password salt
//80bd941066ddd3a27849f192ff5dded43ca32b488aad243bf0196d unk
//3231312e34332e3134362e3636003ccc00000000 ipAddr


//65000000 
//35613038303630616c663538696c626338357a36306c353862667a36303538626364313000 password salt
//80bd941066ddd3a27849f192ff5dded43ca32b488aad243bf0196d unk
//3231312e34332e3134362e3636003ccc00000000 ipAddr